import 'package:flutter/material.dart';
import 'package:flutter_massage/common_lib/extension/ex_widget.dart';
import 'package:flutter_massage/common_lib/index.dart';
import 'package:flutter_massage/common_lib/routers/route.dart';
import 'package:flutter_massage/common_lib/utils/js_channel_operation.dart';
import 'package:flutter_massage/values/index.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:webview_flutter/webview_flutter.dart';

class CommonDialog {
  static bool loginIsShowing = false;

  static void oneButton({
    String title = '温馨提示',
    String description = '',
    String confirmTitle = '确定',
    VoidCallback? onConfirm,
    String? tag,
    VoidCallback? onDismiss,
  }) {
    SmartDialog.show(
      builder: (context) {
        return Container(
          width: ScreenUtil().screenWidth - 30.w,
          decoration: BoxDecoration(color: AppColors.white, borderRadius: BorderRadius.circular(8.w)),
          padding: EdgeInsets.fromLTRB(20.w, 15.w, 20.w, 25.w),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              if (title.notEmpty) ...[
                TextWidget.moreLine(text: title, fontSize: 18, color: AppColors.textLevelOne),
                SizedBox(height: 20.w),
              ],
              if (description.notEmpty) ...[
                TextWidget.moreLine(text: description, fontSize: 15, color: AppColors.textLevelOne),
                SizedBox(height: 25.w),
              ],
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 7.w),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Expanded(
                      child: ButtonWidget.text(
                        confirmTitle,
                        textColor: AppColors.white,
                        borderColor: AppColors.primaryGreen,
                        backgroundColor: AppColors.primaryGreen,
                        borderWidth: 0.5,
                        borderRadius: 45.w,
                        height: 45.w,
                        alignment: Alignment.center,
                        onTap: () {
                          onConfirm?.call();
                        },
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        );
      },
      backDismiss: false,
      clickMaskDismiss: false,
      tag: tag,
      onDismiss: onDismiss,
    );
  }

  static void twoButton({
    String title = '温馨提示',
    String description = '',
    String cancelTitle = '取消',
    String confirmTitle = '确定',
    VoidCallback? onCancel,
    VoidCallback? onConfirm,
    Widget? descriptionWidget,
    String? tag,
    VoidCallback? onDismiss,
  }) {
    SmartDialog.show(
      builder: (context) {
        List<Widget> children = [];

        /// 标题
        if (title.notEmpty) {
          children.addAll([
            TextWidget.moreLine(text: title, fontSize: 17, color: AppColors.textLevelOne, fontWeight: FontWeight.bold),
            SizedBox(height: 12.w),
          ]);
        }

        /// 描述
        if (descriptionWidget != null) {
          children.addAll([
            descriptionWidget,
            SizedBox(height: 20.w),
          ]);
        } else {
          if (description.notEmpty) {
            children.addAll([
              TextWidget.moreLine(text: description, fontSize: 13, color: AppColors.textLevelOne),
              SizedBox(height: 20.w),
            ]);
          }
        }

        return Container(
          width: ScreenUtil().screenWidth - 60.w * 2,
          decoration: BoxDecoration(color: AppColors.white, borderRadius: BorderRadius.circular(12.w)),
          padding: EdgeInsets.fromLTRB(16.w, 20.w, 16.w, 12.w),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              if (children.isNotEmpty) ...children,
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Expanded(
                    child: ButtonWidget.text(
                      cancelTitle,
                      textColor: AppColors.textLevelOne,
                      borderRadius: 8.w,
                      height: 38.w,
                      alignment: Alignment.center,
                      gradient: LinearGradient(
                        begin: Alignment.centerLeft,
                        end: Alignment.centerRight,
                        stops: const [0, 1],
                        colors: ['DDDDDD'.toColor, 'B7B7B7'.toColor],
                      ),
                      onTap: () {
                        SmartDialog.dismiss();
                        onCancel?.call();
                      },
                    ),
                  ),
                  SizedBox(width: 10.w),
                  Expanded(
                    child: ButtonWidget.text(
                      confirmTitle,
                      textColor: AppColors.white,
                      borderRadius: 8.w,
                      height: 38.w,
                      alignment: Alignment.center,
                      gradient: CommonDecoration.xydGradient,
                      onTap: () async {
                        await SmartDialog.dismiss();
                        onConfirm?.call();
                      },
                    ),
                  ),
                ],
              ),
            ],
          ),
        );
      },
      backDismiss: false,
      clickMaskDismiss: false,
      tag: tag,
      onDismiss: onDismiss,
    );
  }

  static void cupertinoTwoButton({
    String title = '温馨提示',
    String description = '',
    String cancelTitle = '取消',
    String confirmTitle = '确定',
    VoidCallback? onCancel,
    VoidCallback? onConfirm,
    String? tag,
    VoidCallback? onDismiss,
  }) {
    SmartDialog.show(
      builder: (context) {
        List<Widget> children = [];

        double titleTop = description.notEmpty ? 21.w : 33.w;
        double titleBottom = description.notEmpty ? 9.w : 33.w;

        /// 标题
        children.add(TextWidget.moreLine(
          text: title,
          fontSize: 18,
          color: AppColors.textLevelOne,
        ).padding(top: titleTop, bottom: titleBottom, left: 20.w, right: 20.w));

        ///描述
        if (description.notEmpty) {
          children.add(TextWidget.moreLine(
            text: description,
            fontSize: 15,
            color: AppColors.textLevelThree,
            textAlign: TextAlign.center,
          ).padding(bottom: 10.w, left: 20.w, right: 20.w));
        }

        return Container(
          width: ScreenUtil().screenWidth - 52.w * 2,
          decoration: BoxDecoration(color: AppColors.white, borderRadius: BorderRadius.circular(7.w)),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              if (children.isNotEmpty) ...children,
              const CommonDivider(),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Expanded(
                    child: ButtonWidget.text(
                      cancelTitle,
                      textColor: AppColors.textLevelOne,
                      fontSize: 18,
                      height: 50.w,
                      alignment: Alignment.center,
                      hasInkWell: false,
                      onTap: () {
                        SmartDialog.dismiss();
                        onCancel?.call();
                      },
                    ),
                  ),
                  Container(
                    width: 1,
                    height: 50.w,
                    color: AppColors.line,
                  ),
                  Expanded(
                    child: ButtonWidget.text(
                      confirmTitle,
                      textColor: '#FF5147'.toColor,
                      fontSize: 18,
                      height: 50.w,
                      alignment: Alignment.center,
                      hasInkWell: false,
                      onTap: () async {
                        await SmartDialog.dismiss();
                        onConfirm?.call();
                      },
                    ),
                  ),
                ],
              ),
            ],
          ),
        );
      },
      backDismiss: false,
      clickMaskDismiss: false,
      tag: tag,
      onDismiss: onDismiss,
    );
  }

  static void showBox({
    required String url,
    double? width,
    double? height,
  }) {
    SmartDialog.show(
      builder: (context) {
        return Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            SizedBox(
              width: width ?? 300.w,
              height: height ?? 300.w,
              child: WebViewDialog(url),
            ),
            ButtonWidget.icon(
              Icon(Icons.close, color: Colors.white, size: 22.w),
              onTap: () => SmartDialog.dismiss(tag: 'webView_dialog'),
              backgroundColor: Colors.black26,
              width: 40.w,
              height: 40.w,
              borderRadius: 40.w,
              alignment: Alignment.center,
            ).paddingTop(15.w),
          ],
        );
      },
      backDismiss: false,
      clickMaskDismiss: false,
      tag: 'webView_dialog',
    );
  }

  // /// 登录弹窗
  // static void showLoginDialog() {
  //   if (loginIsShowing) return;
  //   loginIsShowing = true;
  //   oneButton(
  //     description: '请重新登录',
  //     confirmTitle: '登录',
  //     onConfirm: () {
  //       loginIsShowing = false;
  //       CommonRoute.openLogin();
  //     },
  //     onDismiss: () => loginIsShowing = false,
  //   );
  // }
}

class WebViewDialog extends StatefulWidget {
  const WebViewDialog(this.url, {super.key});

  final String url;

  @override
  State<WebViewDialog> createState() => _WebViewDialogState();
}

class _WebViewDialogState extends State<WebViewDialog> {
  late final WebViewController controller;

  @override
  void initState() {
    super.initState();
    controller = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setBackgroundColor(Colors.white)
      ..addJavaScriptChannel(
        'startFunction',
        onMessageReceived: JsChannelOperation.onMessageReceived,
      )
      ..loadRequest(Uri.parse(widget.url));
  }

  @override
  Widget build(BuildContext context) {
    return WebViewWidget(controller: controller);
  }
}
